package cn.net.wanji.utils;

/**
 * @author weining
 * @version 1.0
 * @date 2023-10-16 8:56
 */
public class FusionUtils {

    /**
     * 比较两个车牌号，返回匹配的位数。
     *
     * @param plate1 第一个车牌号
     * @param plate2 第二个车牌号
     * @return 匹配的位数
     */
    // <summary>
    // 计算车牌匹配度
    // </summary>
    // <param name="plate1">进行对比的第一个车牌</param>
    // <param name="plate2">进行对比的第二个车牌</param>
    // <param name="Type">匹配度计算方式：0表示不采取模糊匹配，1表示采取模糊匹配</param>
    // <output>返回值类型int，0~100</output>
    //@xin 车牌7位，新能源8位
    //@xin 车牌中不存在大写的O和I，只有阿拉伯数字0和1，大写的O据杨光说在广东出现过
    public static int nCalcPlateMatchPoint(String plate1, String plate2, int Type) {
        int i;
        int j;
        int nonmatchnum = 0;
        j = 0;
        int platelength = 0;
        if (plate1 != null && !"".equals(plate1) && plate2 != null && !"".equals(plate2)) {
            //去掉因CPC发卡出现的车牌号中带‘临’‘绿’字 wnn
            plate1 = plate1.replace("临", "").replace("绿", "");
            plate2 = plate2.replace("临", "").replace("绿", "");
            char[] plate1Char = plate1.toCharArray();
            char[] plate2Char = plate2.toCharArray();
            platelength = Math.min(plate1.length(), plate2.length());
            if (platelength > 4 && !"默A00000".equals(plate1) && !"默A00000".equals(plate2))        //&xin 比较车牌的最短长度，至少为5个 -没什么依据
            {
                for (i = 0; i < platelength; i++) {
                    if (plate1Char[i] == plate2Char[i]) {
                        j = j + 100;
                    } else {
                        nonmatchnum += 1;
                        if (Type == 1)      //&xin 以下比较从何而来 -经验，分值给的也没有具体的依据
                        {
                            //防止发卡写错
                            if ((plate1Char[i] == '1' || plate1Char[i] == 'I' || plate1Char[i] == 'i') &&
                                    (plate2Char[i] == '1' || plate2Char[i] == 'I' || plate2Char[i] == 'i')) {
                                j = j + 100;
                                if (nonmatchnum > 0) {
                                    nonmatchnum -= 1;
                                }
                            } else if ((plate1Char[i] == '0' || plate1Char[i] == 'D' || plate1Char[i] == 'Q') &&
                                    (plate2Char[i] == '0' || plate2Char[i] == 'D' || plate2Char[i] == 'Q')) {
                                j = j + 75;
                            } else if ((plate1Char[i] == '0' || plate1Char[i] == 'D' || plate1Char[i] == 'U') &&
                                    (plate2Char[i] == '0' || plate2Char[i] == 'D' || plate2Char[i] == 'U')) {
                                j = j + 75;
                            } else if ((plate1Char[i] == '0' || plate1Char[i] == 'O') &&
                                    (plate2Char[i] == '0' || plate2Char[i] == 'O'))             //&xin  不存在大写字母O，此处是否应该按100算 -实际上车牌有O，杨光说在广东出现过
                            {
                                j = j + 75;
                            } else if ((plate1Char[i] == '2' || plate1Char[i] == 'Z') &&
                                    (plate2Char[i] == '2' || plate2Char[i] == 'Z')) {
                                j = j + 70;
                            } else if ((plate1Char[i] == '5' || plate1Char[i] == 'S') &&
                                    (plate2Char[i] == '5' || plate2Char[i] == 'S')) {
                                j = j + 65;
                            } else if ((plate1Char[i] == '8' || plate1Char[i] == 'B') &&
                                    (plate2Char[i] == '8' || plate2Char[i] == 'B')) {
                                j = j + 65;
                            } else if ((plate1Char[i] == 'E' || plate1Char[i] == 'F') &&
                                    (plate2Char[i] == 'E' || plate2Char[i] == 'F')) {
                                j = j + 40;
                            } else if ((plate1Char[i] == 'G' || plate1Char[i] == '0') &&
                                    (plate2Char[i] == 'G' || plate2Char[i] == '0')) {
                                j = j + 40;
                            } else if ((plate1Char[i] == 'G' || plate1Char[i] == '6') &&
                                    (plate2Char[i] == 'G' || plate2Char[i] == '6')) {
                                j = j + 30;
                            } else if ((plate1Char[i] == 'T' || plate1Char[i] == 'Y') &&
                                    (plate2Char[i] == 'T' || plate2Char[i] == 'Y')) {
                                j = j + 30;
                            } else if ((plate1Char[i] == '1' || plate1Char[i] == '7' || plate1Char[i] == 'T') &&
                                    (plate2Char[i] == '1' || plate2Char[i] == '7' || plate2Char[i] == 'T')) {
                                j = j + 30;
                            } else if ((plate1Char[i] == 'Z' || plate1Char[i] == '7') &&
                                    (plate2Char[i] == 'Z' || plate2Char[i] == '7')) {
                                j = j + 40;
                            } else if ((plate1Char[i] == 'V' || plate1Char[i] == 'Y') &&
                                    (plate2Char[i] == 'V' || plate2Char[i] == 'Y')) {
                                j = j + 30;
                            } else if ((plate1Char[i] == '4' || plate1Char[i] == 'A') &&
                                    (plate2Char[i] == '4' || plate2Char[i] == 'A')) {
                                j = j + 30;
                            } else if ((plate1Char[i] == 'H' || plate1Char[i] == 'N') &&
                                    (plate2Char[i] == 'H' || plate2Char[i] == 'N')) {
                                j = j + 30;
                            } else if (i == 0) {
                                if ((plate1Char[i] == '浙' || plate1Char[i] == '湘') &&
                                        (plate2Char[i] == '浙' || plate2Char[i] == '湘')) {
                                    j = j + 70;
                                } else if ((plate1Char[i] == '京' || plate1Char[i] == '琼') &&
                                        (plate2Char[i] == '京' || plate2Char[i] == '琼')) {
                                    j = j + 70;
                                } else if ((plate1Char[i] == '鲁' || plate1Char[i] == '晋') &&
                                        (plate2Char[i] == '鲁' || plate2Char[i] == '晋')) {
                                    j = j + 70;
                                } else if ((plate1Char[i] == 'H' || plate1Char[i] == '川') &&
                                        (plate2Char[i] == 'H' || plate2Char[i] == '川'))         //&xin 车牌第一个字符还有数字的吗 -军车类
                                {
                                    j = j + 70;
                                } else if ((plate1Char[i] == 'E' || plate1Char[i] == '甘') &&
                                        (plate2Char[i] == 'E' || plate2Char[i] == '甘')) {
                                    j = j + 70;
                                } else if ((plate1Char[i] == 'Z' || plate1Char[i] == '云') &&
                                        (plate2Char[i] == 'Z' || plate2Char[i] == '云')) {
                                    j = j + 70;
                                } else {
                                    j = j + 50;
                                }
                            }
                        } else {
                            //防止发卡写错
                            // &xin 车牌中只有1，没有I和i，此处不应该+100吗 -应该+100  //@20200319
                            if ((plate1Char[i] == '1' || plate1Char[i] == 'I' || plate1Char[i] == 'i') &&
                                    (plate2Char[i] == '1' || plate2Char[i] == 'I' || plate2Char[i] == 'i')) {
                                j = j + 100;
                                if (nonmatchnum > 0) {
                                    nonmatchnum -= 1;
                                }
                            }
                        }
                    }
                }
                /*******最多只允许错4个字符********/
                if (nonmatchnum > 4)                        //@xin 单个数字基本表示错了几个字符
                {
                    j = nonmatchnum;
                } else {
                    if (plate1.length() > plate2.length()) {
                        j /= plate1.length();
                    } else if (plate1.length() < plate2.length()) {
                        j /= plate2.length();
                    } else {
                        j /= platelength;
                    }
                }
            }
        }
        if ((plate1 != null ? plate1.length() : 0) != (plate2 != null ? plate2.length() : 0)) {
            j = j - Math.abs(plate1.length() - plate2.length()) * 10;
        }
        return j;
    }

    public static void main(String[] args) {
        String licensePlate1 = "贵A10ER5";
        String licensePlate2 = "京A10ER5";
        int matchCount = nCalcPlateMatchPoint(licensePlate1, licensePlate2, 1);
        System.out.println("匹配的结果: " + matchCount);
    }

}
